<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
     
      <el-form-item label="标签" prop="filedCnName">
        <el-input
          v-model="queryParams.filedCnName"
          placeholder="请输入查询数值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['form:autotable:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['form:autotable:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['form:autotable:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['form:autotable:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

	
	<el-table :data="dataList" style="width: 100%"  @selection-change="handleSelectionChange">
		<el-table-column type="selection" width="55" align="center" />
	    <el-table-column v-for="(item,key) in filedsList" :key="key" :prop="item.value" :label="item.name">
	        <template slot-scope="scope">
	          <span >{{scope.row[scope.column.property]}}</span>
	        </template>
	    </el-table-column>
	</el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form"  label-width="80px">
        <el-form-item v-for="(item,key) in filedsList"  :key="key" :prop="item.value" :label="item.name">
            <el-input @input="onInput()" placeholder="请输入信息" v-model="form[filedsList[key].value]" :id="filedsList[key].value" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFileds, getFileds, delFileds, autoUpdateFileds, autoAddFileds, exportAutoData  } from "@/api/form/synctable";


export default {
  name: "Data",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: false,
      // 显示搜索条件
      showSearch: true,
	  isAdd: false,
      // 总条数
      total: 0,
	  //表头数据
	  filedsList: [],
      // 字典表格数据
      dataList: [],
      // 默认字典类型
      defaultDictType: "",
      // 弹出层标题
      title: "",
	  TABLE_ID: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      // 类型数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        dictName: undefined,
        dictType: undefined,
        status: undefined
      },
      // 表单参数
      form: {}
    };
  },
  created() {
	//表ID
    const tableId = this.$route.params && this.$route.params.tableId;
	this.form.TABLE_ID=tableId;
	this.TABLE_ID =tableId;
	this.queryParams.bId=tableId;
    this.getHeads(tableId);
    
  },
  methods: {
    /** 查询获取表头信息 */
    getHeads(tableId) {
	  this.queryParams.bId=tableId;
      this.loading = true;
      listFileds(this.queryParams).then(response => {
        this.filedsList = response.title;
		this.dataList=response.value;
       // this.total = response.total;
        this.loading = false;
      });
    },onInput(){
     this.$forceUpdate();
},
	/** 查询数据信息列表 */
	getList(tableId) {
	  this.queryParams.bId=tableId;
	  this.loading = true;
	  listFileds(this.queryParams).then(response => {
	    this.filedsList = response.title;
		this.dataList=response.value;
	   // this.total = response.total;
	    this.loading = false;
	  });
	},
    // 数据状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      // this.form = {
      //   T_ID: undefined
       
      // };
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(this.queryParams.bId);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.dictType = this.defaultDictType;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
	  this.isAdd=true
      this.reset();
      this.open = true;
      this.title = "添加数据";
	  //遍历
	  this.filedsList.forEach(item =>{
		const value= item.value;
		this.form.[item.value] ="";
	   })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.T_ID)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
	  this.isAdd=false;
	  this.queryParams.fId = this.ids[0];
      getFileds(this.queryParams).then(response => {
		const filedsList =response.title ;
		const dataList=response.value;
		this.form=dataList[0];
		this.form.TABLE_ID=this.TABLE_ID;
		
		
		// dataList.forEach(item =>{
		// 		const value= item.value;
		// 		this.form.[item.value] =value;
		//  })
		
     
        this.open = true;
        this.title = "修改数据";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.isAdd!= true) {
			  this.form.T_ID=this.ids[0];
            autoUpdateFileds(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList(this.queryParams.bId);
              }
            });
          } else {
            autoAddFileds(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList(this.queryParams.bId);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictCodes = row.dictCode || this.ids;
	  const tableName =this.TABLE_ID;
      this.$confirm('是否确认删除字典编码为"' + dictCodes + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
			dictCodes.push(tableName);
          return delFileds(dictCodes);
        }).then(() => {
            this.getList(this.queryParams.bId);
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAutoData(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>